package cn.szzsi._admin;

import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Brand;
import cn.szzsi.common.model.Product;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.upload.UploadFile;

import java.util.List;


/**
 * Created by yuanzheng on 2016/9/23.
 */
public class BrandController extends BaseAdminController<Brand> {
    private Brand brand;
    private String shopId;
    private UploadFile logo;
    // 列表
    public void list() {
        render("brand_list.html");
    }
    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        System.out.println(shopId);
        List<Brand> lpa = Brand.dao.find("select * from mall_brand where uid = ? order by createDate desc",shopId);
        renderJson(lpa);
    }

    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        render("brand_input.html");
    }

    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if (StrKit.notBlank(id)){
            setAttr("brand", Brand.dao.findById(id));
        }
        setAttr("shopId",shopId);
        render("brand_input.html");
    }

    // 保存
    //@Before(BusinessBrandValidator.class)
    public void save() {
        shopId=getAttr("shopId");
        brand = getModel(Brand.class);
        brand.set("uid",shopId);
        saved(brand);
        renderJson(Ret.ok("info","添加成功!").set("url","list"));
    }

    // 更新
    public void update() {
        brand = getModel(Brand.class);
        updated(brand);
        renderJson(Ret.ok("info","修改成功!").set("url","list"));
    }

    // 删除
    public void delete() {
        String id=getPara("id");
        List<Product> productList = Brand.dao.findById(id).getProductList();
        if (productList != null && productList.size() > 0){
            renderJson(Ret.fail("info","品牌在产品列表中存在，删除失败"));
            return;
        }
        Brand.dao.delete(id);
        System.out.println(Ret.ok());
        renderJson(Ret.ok());

    }
}
